<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/admin/adminPage/AdminIndex' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>酒店房间信息管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 80%">
      <el-table-column prop="id" label="编号" width="80px" align="center"></el-table-column>
      <el-table-column prop="hotelName" label="酒店名字"  align="center"
                       :show-overflow-tooltip="true">
      </el-table-column>
      <el-table-column prop="hotelId" label="酒店Id" width="80px" align="center">
      </el-table-column>
      <el-table-column prop="roomName" label="房间类型" width="200px" align="center"
                       :show-overflow-tooltip="true">
      </el-table-column>
      <el-table-column prop="roomId" label="房间ID" width="80px" align="center">
      </el-table-column>
      <el-table-column prop="number" label="房间数量" width="80px" align="center">
      </el-table-column>
      <el-table-column prop="price" label="房间价格" width="150px" align="center">
      </el-table-column>
      <el-table-column label="操作" width="200px" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     @click="openEditDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     @click="openDeleteConfirm(scope.row)">
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="修改酒店房间信息" style="width: 1200px;margin-left: auto;margin-right: auto" :rules="rules" :visible.sync="dialogFormVisible">
      <el-form :model="ruleForm" label-width="150px" class="demo-ruleForm">
        <el-form-item label="酒店名字">
          <el-input :disabled="true" v-model="ruleForm.hotelName" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="酒店Id">
          <el-input :disabled="true" v-model="ruleForm.hotelId" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="房间类型" prop="roomName">
          <el-input v-model="ruleForm.roomName" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="房间ID">
          <el-input :disabled="true" v-model="ruleForm.roomId" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="房间数量">
          <el-input v-model="ruleForm.number" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="房间价格" prop="price">
          <el-input v-model="ruleForm.price" style="width: 300px"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button ref="upload" @click="cancelEditForm()">取 消</el-button>
        <el-button type="primary" @click="submitEditForm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data(){
    return{
      tableData:[],
      dialogFormVisible:false,
      hotelRoomInfoID:'',
      ruleForm:{
        hotelName:'',
        hotelId:'',
        roomName:'',
        roomId:'',
        number:'',
        price:''
      },
      rules:{
        roomName:[
          {required: true, message: '请输入房间类型', trigger: 'blur'}
        ],
        price:[
          {required: true, message: '请输入价格', trigger: 'blur'}
        ]
      }
    }
  },
  methods:{
    submitEditForm(){
      let url = 'http://localhost:9580/hotels/admin/'+this.hotelRoomInfoID+'/updateAdminHotelRoomInfoById';
      console.log("url"+url);

      let formData=this.qs.stringify(this.ruleForm);
      console.log('formData'+formData);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('aJwt')}})
          .post(url,formData).then((response) =>{
            let responseBody=response.data;
            if (responseBody.state==20000){
              this.dialogFormVisible=false;
              this.$message({
                message: '修改酒店房间信息成功！',
                type: 'success'
              });
              this.loadHotelRoomInfoList();
            }else if (responseBody.state == 40400) {
              this.$alert(responseBody.message, '警告', {
                confirmButtonText: '确定',
                callback: action => {
                  this.dialogFormVisible = false;
                  this.loadHotelRoomInfoList();
                }
              });
            } else {
              this.$message.error(responseBody.message);
            }
      })
    },
    cancelEditForm(){
      this.dialogFormVisible=false;
      this.loadHotelRoomInfoList();
    },
    loadHotelRoomInfoList(){
      let url = 'http://localhost:9580/hotels/admin/getAdminHotelRoomInfoList';
      console.log("url"+url);
      this.axios
          .create({'headers':{'Authorization':localStorage.getItem('aJwt')}})
          .get(url).then((response) =>{
        let responseBody=response.data;
        this.tableData=responseBody.data;
        console.log(this.tableData);
      })
    },
    openEditDialog(item) {
      let url = 'http://localhost:9580/hotels/admin/'+item.id+'/getAdminHotelRoomInfoById';
      this.hotelRoomInfoID=item.id;
      this.dialogFormVisible = true;
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('aJwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        this.ruleForm=responseBody.data;
      });
    },
    openDeleteConfirm(item) {
      let message = '此操作将永久删除【' + item.hotelName + '】【'+item.roomName+'】的房间信息信息，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(item);
      }).catch(() => {
      });
    },
    handleDelete(item) {
      let url = 'http://localhost:9580/hotels/admin/'+item.id+'/deleteHotelRoomInfo';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('aJwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '删除酒店房间信息数据成功！',
            type: 'success'
          });
          this.loadHotelRoomInfoList();
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadHotelRoomInfoList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    }
  },
  mounted() {
    this.loadHotelRoomInfoList();
  }
}
</script>

<style scoped>

</style>